package com.bayss.core.service.homepage;

import com.bayss.core.entity.homepage.ActivityInfo;
import com.bayss.core.entity.homepage.ActivityInfoExample;
import com.bayss.core.entity.homepage.RotationChart;
import com.bayss.core.entity.homepage.RotationChartExample;
import com.bayss.core.entity.homepage.mapper.RotationChartMapper;
import com.bayss.core.util.CollectionUtils;
import com.bayss.core.util.FileUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * program: doko
 * author: bsworld.xie
 * create: 2019-11-10 16:03
 * description:
 */
@Service
public class RotationChartService {
    @Autowired
    RotationChartMapper rotationChartMapper;

    public void addRotationChart(RotationChart rotationChart) {
        rotationChart.setCreateTime(new Date());
        rotationChart.setUpdateTime(new Date());
        rotationChart.setStatus(1);
        rotationChart.setPicCn(FileUtil.formatImgUrl(rotationChart.getPicCn()));
        rotationChart.setPicEn(FileUtil.formatImgUrl(rotationChart.getPicEn()));
        rotationChart.setPicKo(FileUtil.formatImgUrl(rotationChart.getPicKo()));
        rotationChart.setDetailPicCn(FileUtil.formatImgUrl(rotationChart.getDetailPicCn()));
        rotationChart.setDetailPicEn(FileUtil.formatImgUrl(rotationChart.getDetailPicEn()));
        rotationChart.setDetailPicKo(FileUtil.formatImgUrl(rotationChart.getDetailPicKo()));
        rotationChartMapper.insertSelective(rotationChart);
    }
    /**
     *
     * 更新轮播图
     * */
    public void updateRotationChart(RotationChart rotationChart) {
        rotationChart.setUpdateTime(new Date());
        if(rotationChart.getPicCn()!=null){
            rotationChart.setPicCn(FileUtil.formatImgUrl(rotationChart.getPicCn()));
        }
        if(rotationChart.getPicEn()!=null){
            rotationChart.setPicEn(FileUtil.formatImgUrl(rotationChart.getPicEn()));
        }
        if(rotationChart.getPicKo()!=null){
            rotationChart.setPicKo(FileUtil.formatImgUrl(rotationChart.getPicKo()));
        }
        if(rotationChart.getDetailPicCn()!=null){
            rotationChart.setDetailPicCn(FileUtil.formatImgUrl(rotationChart.getDetailPicCn()));
        }
        if(rotationChart.getDetailPicEn()!=null){
            rotationChart.setDetailPicEn(FileUtil.formatImgUrl(rotationChart.getDetailPicEn()));
        }
        if(rotationChart.getDetailPicKo()!=null){
            rotationChart.setDetailPicKo(FileUtil.formatImgUrl(rotationChart.getDetailPicKo()));
        }
        rotationChartMapper.updateByPrimaryKeySelective(rotationChart);
    }


    public RotationChart getRotationChartById(Integer id) {
        RotationChartExample example = new RotationChartExample();
        example.createCriteria().andIdEqualTo(id);
        List<RotationChart> rotationCharts = rotationChartMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(rotationCharts)) {
            return rotationCharts.get(0);
        }
        return null;
    }


    public PageInfo<RotationChart> getRotationChartByPage(Integer status ,Integer page, Integer pageSize) {
        RotationChartExample example = new RotationChartExample();
        example.setOrderByClause("create_time desc");
        if(status!=null){
            example.createCriteria().andStatusEqualTo(status);
        }
        PageHelper.startPage(page, pageSize);
        PageInfo<RotationChart> result = PageHelper.startPage(page, pageSize).doSelectPageInfo(() -> rotationChartMapper.selectByExample(example));
        return result;
    }


    public void deleteById(Integer id) {
        rotationChartMapper.deleteByPrimaryKey(id);
    }
}
